import { ReqUser } from '@common/auth';
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, ObjectId } from 'mongoose';
import { FocusUserFocusPhotosDto } from './dto/focus-userFocusPhotos.dto';
import { ListUserFocusPhotosDto } from './dto/list-userFocusPhotos.dto';
import { UserFocusPhotos, UserFocusPhotosDocument } from './schemas/userFocusPhotos.schema';

@Injectable()
export class UserFocusPhotosService {
    constructor(
        @InjectModel(UserFocusPhotos.name) private userFocusPhotosModel: Model<UserFocusPhotosDocument>
    ) {}

    async focus(input: FocusUserFocusPhotosDto, reqUser: ReqUser) {
        const createdCat = new this.userFocusPhotosModel(input);
        createdCat.user = reqUser._id;
        const res = await createdCat.save();
        return res;
    }
    async unfocus(input: FocusUserFocusPhotosDto, reqUser: ReqUser) {
        return this.userFocusPhotosModel
            .findOneAndRemove({
                photos: input.photos,
                user: reqUser._id,
            })
            .exec();
    }
    async list(dto: ListUserFocusPhotosDto, reqUser: ReqUser) {
        const { page = 0, psize = 20 } = dto.limit || {};
        const res = await this.userFocusPhotosModel
            .find({ user: reqUser._id })
            .populate({
                path: 'photos',
                populate: [
                    { path: 'user', select: 'nickname avatar desc' },
                    { path: 'profile', select: 'totalDown' },
                ],
            })
            .skip(page * psize)
            .limit(psize)
            .sort('-createdAt')
            .exec();
        return res.map(it => it.photos);
    }
}
